home *** CD-ROM | disk | FTP | other *** search
/ Aminet 33 / Aminet 33 - October 1999.iso / Aminet / util / misc / resetserver.lha / ResetServer / ResetClient / ResetClient.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-06-19  |  9.6 KB  |  463 lines

  1. /* ResetClient.c (c) 1997 Paweî Marciniak <pmarciniak@lodz.home.pl>*/
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <exec/execbase.h>
  5. #include <exec/types.h>
  6. #include <exec/memory.h>
  7. #include <intuition/intuition.h>
  8. #include <libraries/gadtools.h>
  9. #include <graphics/text.h>
  10. #include <graphics/gfxbase.h>
  11. #include <graphics/scale.h>
  12. #include <proto/dos.h>
  13. #include <proto/exec.h>
  14. #include <proto/intuition.h>
  15. #include <proto/graphics.h>
  16. #include <proto/gadtools.h>
  17. #include <proto/diskfont.h>
  18. #include <proto/dos.h>
  19. #include <stdlib.h>
  20.  
  21. #include "APP_globals.h"
  22. #include "resetserver.h"
  23.  
  24. extern struct ExecBase *SysBase;
  25.  
  26. ULONG signal=0L;
  27. ULONG sigwin=0L;
  28. ULONG sigmsg=0L;
  29. UBYTE TextBufor[256];
  30.  
  31. /* Rozmiar Okna */
  32.  
  33.     WORD Width=440;
  34.     WORD Height=22;
  35.     WORD MinWidth=50;
  36.     WORD MinHeight=30;
  37.     WORD MaxWidth=1600;
  38.     WORD MaxHeight=1200;
  39.  
  40. /* Zmienne ResetDemo */
  41.  
  42. #define PROGNAME "ResetClientDemo...!"
  43.  
  44. struct MsgPort *msgport, *destport;
  45. struct RWMessage sendmsg, sendmsg2, sendmsg3, *msg;
  46.  
  47. /* Tytaj zaczynajâ sië wszystkie poûyteczne funkcje */
  48.  
  49.  
  50. /* Funkcja otwiera okreôlony font */
  51.  
  52. int MakeGadget( void )
  53. {
  54.     if ( g = CreateContext( &APP_GList ))
  55.     {
  56. /* Warning */
  57.         ng.ng_TopEdge        =    (ComputeY( 5 ) + OffY );
  58.         ng.ng_LeftEdge    =    ComputeX( 10 ) + OffX;
  59.         ng.ng_Width            =    ComputeX( 150 );
  60.         ng.ng_Height        =    ComputeY( 12 );
  61.         ng.ng_TextAttr        =    &ScreenFont;
  62.         ng.ng_GadgetText=    "Welcom";
  63.         ng.ng_GadgetID    =    GD_BUTTON;
  64.         ng.ng_Flags             =    PLACETEXT_IN;
  65.         ng.ng_VisualInfo    =    VisualInfo;
  66.         ng.ng_UserData        =    NULL;
  67.         if(!(gad_button = g = CreateGadget( BUTTON_KIND, g, &ng, GT_Underscore, '_', TAG_DONE )))
  68.             return( FALSE );
  69.  
  70. /* Text komunikaty */
  71.         ng.ng_TopEdge      =    (ComputeY( 5 ) + OffY);
  72.         ng.ng_LeftEdge    =    ComputeX( 170) + OffX;
  73.         ng.ng_Width            =    ComputeX( 260 );
  74.         ng.ng_Height        =    ComputeY( 12 );
  75.         ng.ng_GadgetText=    "";
  76.         ng.ng_GadgetID    =    GD_TEXT;
  77.         if(!(gad_text = g = CreateGadget( TEXT_KIND, g, &ng, GTTX_Text, (ULONG)TextBufor,
  78.                                                                                                      GTTX_Justification, GTJ_LEFT,
  79.                                                                                                      GTTX_Border, TRUE,
  80.                                                                                                      TAG_DONE )))
  81.             return( FALSE );
  82.         return( TRUE );
  83.     }
  84. return( FALSE );
  85. }
  86.  
  87. int OpenFonts( void )
  88. {
  89.     if (!(APP_Font=OpenDiskFont( &ScreenFont )))
  90.         return( FALSE );
  91.     return( TRUE );
  92. }
  93.  
  94.  
  95. /* Funkcja zamyka otwarty font */
  96.  
  97. void CloseFonts( void )
  98. {
  99.     if ( APP_Font )    
  100.     {
  101.         CloseFont( APP_Font );
  102.         APP_Font=NULL;
  103.     }
  104. }
  105.  
  106.  
  107. UWORD ComputeX( UWORD value )
  108. {
  109.     return(( UWORD )((( FontX * value ) + 4 ) / 8 ));
  110. }
  111.  
  112.  
  113. UWORD ComputeY( UWORD value )
  114. {
  115.     return(( UWORD )((( FontY * value ) + 4 ) / 8 ));
  116. }
  117.  
  118.  
  119. void ComputeFont( void )
  120. {
  121.     Font = &ScreenFont;
  122.     Font->ta_Name = (STRPTR)Screen->RastPort.Font->tf_Message.mn_Node.ln_Name;
  123.     Font->ta_YSize = FontY = Screen->RastPort.Font->tf_YSize;
  124.     FontX = Screen->RastPort.Font->tf_XSize;
  125.     OffX = Screen->WBorLeft;
  126.     OffY = Screen->RastPort.TxHeight + Screen->WBorTop + 1;
  127.     return;
  128. }
  129.  
  130.  
  131. /* Funkcja otwiera okno */
  132.  
  133. int OpenAPP_Display( WORD Left, WORD Top )
  134. {
  135.  
  136.     if(!(APP_Window=OpenWindowTags( 0,
  137.         WA_Left,                Left,
  138.         WA_Top,                    Top,
  139.         WA_Width,                ComputeX(Width) + OffX + Screen->WBorRight,
  140.         WA_Height,            ComputeY(Height) + OffY + Screen->WBorBottom,
  141.         WA_MinWidth,        ComputeX(MinWidth) + OffX + Screen->WBorRight,
  142.         WA_MinHeight,        ComputeX(MinHeight) + OffY + Screen->WBorBottom,
  143.         WA_MaxWidth,        ComputeX(MaxWidth) + OffX + Screen->WBorRight,
  144.         WA_MaxHeight,        ComputeY(MaxHeight) + OffY + Screen->WBorBottom,
  145.         WA_Title,                APP_TitleWindow,
  146.         WA_Flags,                WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH,
  147.         WA_IDCMP,                IDCMP_CLOSEWINDOW | IDCMP_REFRESHWINDOW | IDCMP_GADGETUP,
  148.         WA_Gadgets,            APP_GList,
  149.         WA_AutoAdjust,    FALSE,
  150.         WA_PubScreen,        (struct Screen *)Screen,
  151.         TAG_DONE)))
  152.             return( FALSE );
  153.     GT_RefreshWindow( APP_Window, NULL );
  154.     sigwin=( 1L << APP_Window->UserPort->mp_SigBit );
  155.     return( TRUE );
  156. }
  157.  
  158.  
  159. /* Funkcja odczytuje komunikat Intuition */
  160.  
  161. LONG ReadIMsg( struct Window *iwnd )
  162. {
  163.     struct IntuiMessage *imsg;
  164.     
  165.     if ( imsg = GT_GetIMsg( iwnd->UserPort ))
  166.     {
  167.         IClass      =    imsg->Class;
  168.         Qualifier    =    imsg->Qualifier;
  169.         Code            =    imsg->Code;
  170.         IObject        =    imsg->IAddress;
  171.  
  172.         GT_ReplyIMsg( imsg );
  173.         return( TRUE );
  174.     }
  175.     return( FALSE );
  176. }
  177.  
  178.  
  179. /* Funkcja zamyka otwarte okno */
  180.  
  181. void CloseAPP_Display(void)
  182. {
  183.     if(APP_Window) 
  184.     {
  185.         Forbid();
  186.         while ( ReadIMsg( APP_Window ))
  187.         ;
  188.         CloseWindow((struct Window *)APP_Window);
  189.         APP_Window=NULL;
  190.         Permit();
  191.     }
  192. }
  193.  
  194.  
  195. /* Funkcja alokuje ekran */
  196.  
  197. int SetupScreen( void )
  198. {
  199.     if (!(Screen = LockPubScreen( PubScreenName )))
  200.         return( FALSE );
  201.  
  202.     ComputeFont();
  203.  
  204.     if(!(OpenFonts()))
  205.         return( FALSE );
  206.  
  207.  
  208.     if (!( VisualInfo = GetVisualInfo( Screen, TAG_DONE )))
  209.         return( FALSE );
  210.  
  211.     return( TRUE );
  212. }
  213.  
  214.  
  215. /* Funkcja dealokuje ekran */
  216.  
  217. void CloseDownScreen( void )
  218. {
  219.     if ( VisualInfo )
  220.     {
  221.         FreeVisualInfo( VisualInfo );
  222.         VisualInfo = NULL;
  223.     }
  224.  
  225.     CloseFonts();
  226.  
  227.     if ( Screen )
  228.     {
  229.         UnlockPubScreen( NULL, Screen );
  230.         Screen = NULL;
  231.     }
  232. }
  233.  
  234.  
  235. /* Funkcja otwiera wszystkie biblioteki */
  236.  
  237. LONG OpenLibraries( void )
  238. {
  239.     if ( !(DosBase = (struct DosLibrary *) OpenLibrary((UBYTE *) "dos.library", 37 )))
  240.         return( FALSE );
  241.     if ( !(IntuitionBase = (struct IntuitionBase *) OpenLibrary((UBYTE *) "intuition.library", 37 )))
  242.         return( FALSE );
  243.     if ( !(GadToolsBase = (struct Library *) OpenLibrary((UBYTE *) "gadtools.library", 37 )))
  244.         return( FALSE );
  245.     if ( !(GfxBase = (struct GfxBase *) OpenLibrary((UBYTE *) "graphics.library" , 37 )))
  246.         return( FALSE );
  247.     if ( !(DiskFontBase = (struct Library *) OpenLibrary((UBYTE *) "diskfont.library" , 37 )))
  248.         return( FALSE );
  249.     return( TRUE );
  250. }
  251.  
  252.  
  253. /* Funkcja zamyka wszystkie biblioteki */
  254.  
  255. void CloseLibraries( void )
  256. {
  257.     if (DiskFontBase)        CloseLibrary( (struct Library *) DiskFontBase );
  258.     if (GfxBase)                CloseLibrary( (struct Library *) GfxBase );
  259.     if (GadToolsBase)        CloseLibrary( (struct Library *) GadToolsBase );
  260.     if (IntuitionBase)    CloseLibrary( (struct Library *) IntuitionBase );
  261.     if (DosBase)                CloseLibrary( (struct Library *) DOSBase );
  262. }
  263.  
  264.  
  265. void ShutDown( void )
  266. {
  267.     CloseAPP_Display();
  268.     FreeGadgets( APP_GList );
  269.     APP_GList = NULL;
  270.     CloseDownScreen();
  271.     CloseLibraries();
  272. }
  273.  
  274.  
  275. /************************/
  276. int CreatePorts( void )
  277. {
  278.     if (!(msgport=CreatePort(NULL, 0L)))
  279.         return( FALSE );
  280.     Forbid();
  281.     if (!(destport=FindPort( RESETSERVERNAME )))
  282.   {
  283.         Permit();
  284.         return( FALSE );
  285.   }
  286.   Permit();
  287.     sigmsg = ( 1L << msgport->mp_SigBit  );
  288.     return( TRUE );
  289. }
  290.  
  291. void DeletePorts( void )
  292. {
  293.     if( msgport )
  294.         DeletePort ( msgport );
  295. }
  296.  
  297.  
  298. /* Gîówna funkcja */
  299.  
  300. int main( int argc, char *argv[] )
  301. {
  302. BOOL running=TRUE;
  303. BOOL done=FALSE;
  304.  
  305.     if(!(OpenLibraries()))
  306.     {
  307.         ShutDown();
  308.         return( FALSE );
  309.     }
  310.     
  311.     if(!(SetupScreen()))
  312.     {
  313.         ShutDown();
  314.         return( FALSE );
  315.     }
  316.  
  317.     if(!MakeGadget())
  318.     {
  319.         ShutDown();
  320.         return( FALSE );
  321.     }
  322.  
  323.     if(!(OpenAPP_Display( 20, 20 )))
  324.     {
  325.         ShutDown();
  326.         return( FALSE );
  327.     }
  328.  
  329.     if(!(CreatePorts()))
  330.     {
  331.         strcpy(TextBufor, "Createports failed");
  332.         GT_SetGadgetAttrs( gad_text, APP_Window, 0, GTTX_Text, (ULONG)TextBufor, TAG_DONE);
  333.     }
  334.  
  335.     SetFont( APP_Window->RPort, APP_Font );
  336.  
  337.     sendmsg.rwm_Message.mn_ReplyPort=msgport;
  338.     sendmsg.rwm_MsgPort=msgport;
  339.     sendmsg.rwm_MsgType=RWM_ADDPORT;
  340.     sendmsg.rwm_ProgName=PROGNAME;
  341.     if( destport )
  342.     {
  343.         strcpy(TextBufor, "Sending RWM_ADDPORT");
  344.         GT_SetGadgetAttrs( gad_text, APP_Window, 0, GTTX_Text, (ULONG)TextBufor, TAG_DONE);
  345.         PutMsg( destport, (struct Message *)&sendmsg );
  346.     }
  347.  
  348.     do
  349.     {
  350.         signal=Wait( sigwin | sigmsg );
  351.  
  352.  
  353.         while ( ReadIMsg( APP_Window ))
  354.         {
  355.             switch ( IClass )
  356.             {
  357.                 case    IDCMP_REFRESHWINDOW:
  358.                     GT_BeginRefresh( APP_Window );
  359.                     GT_EndRefresh( APP_Window, TRUE );
  360.                 break;
  361.  
  362.                 case    IDCMP_CLOSEWINDOW:
  363.                     sendmsg3.rwm_Message.mn_ReplyPort=msgport;
  364.                     sendmsg3.rwm_MsgPort=msgport;
  365.                     sendmsg3.rwm_MsgType=RWM_REMPORT;
  366.                     sendmsg3.rwm_ProgName=PROGNAME;
  367.                     if( destport )
  368.                     {
  369.                         strcpy(TextBufor, "Sending RWM_REMPORT");
  370.                         GT_SetGadgetAttrs( gad_text, APP_Window, 0, GTTX_Text, (ULONG)TextBufor, TAG_DONE);
  371.                         PutMsg( destport, (struct Message *)&sendmsg3 );
  372.                     }
  373.                     else
  374.                         running=FALSE;
  375.                 break;
  376.  
  377.                 case    IDCMP_GADGETUP:
  378.                     switch ( IObject->GadgetID )
  379.                     {
  380.                         case    GD_BUTTON:
  381.                             strcpy(TextBufor, "Welcom to ResetClient");
  382.                             GT_SetGadgetAttrs( gad_text, APP_Window, 0, GTTX_Text, (ULONG)TextBufor, TAG_DONE);
  383.                         break;
  384.                     }
  385.                 break;
  386.  
  387.                 default:
  388.                 break;
  389.             }
  390.         }
  391.  
  392.         while( msg=(struct RWMessage *)GetMsg( msgport ))
  393.         {
  394.             switch( msg->rwm_Message.mn_Node.ln_Type )
  395.             {
  396.                 case    NT_MESSAGE:
  397.                     switch( msg->rwm_MsgType )
  398.                     {
  399.                         case    RWM_RESETWARNING:
  400.                             strcpy(TextBufor, "Message ResetWarning");
  401.                             GT_SetGadgetAttrs( gad_text, APP_Window, 0, GTTX_Text, (ULONG)TextBufor, TAG_DONE);
  402.                             done=TRUE;
  403.                             running=FALSE;
  404.                         break;
  405.  
  406.                         default:
  407.                         break;
  408.                     }
  409.                 ReplyMsg((struct Message *)msg);
  410.                 break;
  411.  
  412.                 case    NT_REPLYMSG:
  413.                     switch( msg->rwm_MsgType )
  414.                     {
  415.                         case    RWM_REMPORT:
  416.                             running = FALSE;
  417.                         break;
  418.                     }
  419.                 break;
  420.             }
  421.         }
  422.  
  423.     } while ( running );
  424.  
  425.     running=TRUE;
  426.  
  427.     if( done )
  428.     {
  429.                             sendmsg2.rwm_Message.mn_ReplyPort=msgport;
  430.                             sendmsg2.rwm_MsgPort=msgport;
  431.                             sendmsg2.rwm_MsgType=RWM_DONE;
  432.                             sendmsg2.rwm_ProgName=PROGNAME;
  433.                             if( destport )
  434.                             {
  435.                                 strcpy(TextBufor, "Sending RWM_DONE");
  436.                                 GT_SetGadgetAttrs( gad_text, APP_Window, 0, GTTX_Text, (ULONG)TextBufor, TAG_DONE);
  437.                                 PutMsg( destport, (struct Message *)&sendmsg2 );
  438.                                 do
  439.                                 {
  440.                                   Wait( sigmsg );
  441.                                         while( msg=(struct RWMessage *)GetMsg( msgport ))
  442.                                         {
  443.                                             switch( msg->rwm_Message.mn_Node.ln_Type )
  444.                                             {
  445.                                                 case    NT_REPLYMSG:
  446.                                                     switch( msg->rwm_MsgType )
  447.                                                     {
  448.                                                         case    RWM_DONE:
  449.                                                             running = FALSE;
  450.                                                         break;
  451.                                                     }
  452.                                                 break;
  453.                                             }
  454.                                         }
  455.                                 } while ( running );
  456.                             }
  457.     }
  458.  
  459.     DeletePorts();
  460.     ShutDown();
  461.     return( TRUE );
  462. }
  463.